using System;
using UnityEngine;
using UnityEngine.UI;

public class BlackholeSkillController : Skill
{
    [SerializeField] private UiSkillTreeSlot blackholeUnlockButton;
    public bool BlackholeUnlocked { get; private set; }
    [SerializeField] private int amountOfAttacks;
    [SerializeField] private float cloneAttackCooldown;
    [SerializeField] private float duration;
    [Space]
    [SerializeField] private GameObject blackholePrefab;
    [SerializeField] private float maxSize;
    [SerializeField] private float growSpeed;
    [SerializeField] private float shrinkSpeed;
    private BlackholeSkill blackholeSkill;
    private float circleColliderRadius;

    protected override void Start()
    {
        base.Start();
        blackholeUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockBlackhole);
    }

    protected override void CheckUnlock()
    {
        UnlockBlackhole();
    }

    private void UnlockBlackhole()
    {
        if (!blackholeUnlockButton.unlocked || BlackholeUnlocked) return;
        BlackholeUnlocked = true;
    }

    public override void Use()
    {
        base.Use();
        GameObject newBlackhole = Instantiate(blackholePrefab, Player.transform.position, Quaternion.identity);
        blackholeSkill = newBlackhole.GetComponent<BlackholeSkill>();
        circleColliderRadius = blackholeSkill.GetComponent<CircleCollider2D>().radius;
        blackholeSkill.SetupBlackhole(maxSize, growSpeed, shrinkSpeed, amountOfAttacks, cloneAttackCooldown, duration);
        AudioManager.Instance.PlaySfx(3, Player.transform);
        AudioManager.Instance.PlaySfx(6, Player.transform);
    }

    public float GetRadius()
    {
        if (blackholeSkill)
        {
            return maxSize / circleColliderRadius;
        }
        return maxSize / 2;
    }

    public bool Completed()
    {
        if (!blackholeSkill)
        {
            return false;
        }
        if (blackholeSkill.canExitState)
        {
            blackholeSkill = null;
            return true;
        }
        return false;
    }
}